//
//  PasswordController.m
//  BalanceCar
//
//  Created by 郑信鸿 on 2017/9/1.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "PasswordController.h"
#import "PwErrorView.h"
#import "PwSucceedView.h"
#import "PwInputView.h"
#import "ByteTool.h"

@interface PasswordController ()<PwInputViewDelegate, PasswordErrorDelegate>

@property(nonatomic, strong)PwSucceedView *succeedView;
@property(nonatomic, strong)PwErrorView *errorView;
@property(nonatomic, strong)PwInputView *inputView;
@property(nonatomic, weak)BaseTipView *tipView;

@property(nonatomic, strong)NSString *password;

@property(nonatomic, copy)CompleteBlock complete;

@property(nonatomic, assign)BOOL success;

@end

@implementation PasswordController


- (instancetype)initWithComplete:(CompleteBlock)complete{
    
    if (self = [super init]) {
        
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.complete = complete;
        
    }
    return self;
}

- (PwErrorView *)errorView{
    
    if (_errorView == nil) {
        
        _errorView = [[PwErrorView alloc] init];
        _errorView.delegate = self;
    }
    
    return _errorView;
}

- (PwSucceedView *)succeedView{
    
    if (_succeedView == nil) {
        
        _succeedView = [[PwSucceedView alloc] init];
    }
    return _succeedView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBAColor(0, 0, 0, 0.4);
    [self.backImageView removeFromSuperview];
    self.inputView = [[PwInputView alloc] init];
    self.inputView.delegate = self;
    [self.view addSubview:self.inputView];
    self.tipView = self.inputView;
    self.success = YES;
    //[BaseSendData sendTravelParamsOrder];
    [self configFrame];
    
}


- (void)pwErrorView:(PwErrorView *)view touchWithBtnType:(PasswordBtnType)type{
    
    if (type == PasswordRetry) {
        
        [self.inputView clearPassword];
        [self replaceTipView:self.inputView];
    }
    if (type == PasswordCallService) {
        //联系客服
        
    }
}

- (void)pwInputView:(PwInputView *)view inputEndWithtext:(NSString *)text{
    
    
    self.password = text;
    
    self.success = YES;
    Byte pwbytes[6] = {};
    [ByteTool hexString:self.password pwbytes:pwbytes];
    [[BaseSendData shareManager] sendConnectPw:pwbytes complete:^(NSInteger code) {
        
        if (code == 1) {
            [self replaceTipView:self.succeedView];
            self.success = NO;
//            [[NSUserDefaults standardUserDefaults] setObject:self.password forKey:[BalanceBuleManager shareManager].connectPeripheral.identifier.UUIDString];
            [self dismissViewControllerAnimated:YES completion:^{
                self.complete(code);
            }];
            
        }else if (code == 100){
            self.complete(code);
            [self.inputView clearPassword];
            
        } else {
            
            self.complete(code);
            
            self.success = NO;
            
            [self replaceTipView:self.errorView];
            
        }
        
    }];
    
    
}


- (void)replaceTipView:(BaseTipView *)tipView{
    
    [self.tipView removeFromSuperview];
    
    self.tipView = tipView;
    
    [self.view addSubview:self.tipView];
    
    [self configFrame];
}

- (void)configFrame{
    
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.mas_offset(0);
        make.width.mas_offset(self.tipView.tip_width);
        make.height.mas_offset(self.tipView.tip_height);
        
    }];
    
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
